<?php 
namespace Home\Controller;
use Think\Controller;

class GoodsordersController extends CommonController {

	/**
	 * [主页展示]
	 * @Author   Axing
	 * @DateTime 2017-08-30
	 * @return   [type]     [description]
	 */
	public function index() {
		$page = $this->page($_POST['pageNum'], $_POST['numPerPage']);
		$list = $this->lists($page['currentPage'], $page['numPerPage'], $_GET, $_POST);
		$this->assign('list', $list);
		$this->assign('pages', $page);
		$this->display();
	}

	/**
	 * [列表信息]
	 * @Author   Axing
	 * @DateTime 2017-08-28
	 * @return   [type]     [description]
	 */
	public function lists($currentPage, $numPerPage, $get, $post) {
		$arr = array_merge($get,$post);
		if($arr['state'] != '') {
			$arr['goodsorders.state'] = $arr['state'];
		}

		if($arr['numbers'] != '') {
			$arr['goodsorders.numbers'] = $arr['numbers'];
		}

		$currentPage = $currentPage==1?0:$currentPage;
		unset($arr['state'], $get, $post, $arr['_'], $arr['numbers']);

		//sql拼接
		$arr = array(
				"field"       => 	"goodsorders.`ordersid`, goodsorders.`state`, car.`goodsname`, car.`fTitle`, car.`sTitle`, car.`price`, goodsorders.`blance_price`, car.`number`, goodsorders.`address`, goodsorders.`xia_dan_datetime`, goods.`images`, goodsorders.`numbers`",
				"currentPage" => 	$currentPage,
				"numPerPage"  => 	$numPerPage,
				"order"       => 	"ordersid DESC",
				"where"	=>	$arr,
			);
		//end
		
		$data = D('Goodsorders')->lists($arr);

		foreach($data as $k => $v) {
			$images = explode(',', $v['images']);
			$data[$k]['images']  = $images[0];
		}
		return $data;
	}

	/**
	 * [分页]
	 * @Author   Axing
	 * @DateTime 2017-08-30
	 * @param    string     $currentPage [description]
	 * @param    string     $numPerPage  [description]
	 * @return   [type]                  [description]
	 */
	public function page($currentPage='', $numPerPage='') {
		$arr = array(
				"field" => "goodsorders.`ordersid`, goodsorders.`state`, car.`goodsname`, car.`fTitle`, car.`sTitle`, car.`price`, goodsorders.`blance_price`, car.`number`, goodsorders.`address`, goodsorders.`xia_dan_datetime`, goods.`images`",
			);
		$data = D('Goodsorders')->lists($arr);
		$pages['total'] = count($data);

		$currentPage = empty($currentPage) || is_null($currentPage)?1:$currentPage;
		$numPerPage = empty($numPerPage) || is_null($numPerPage)?100:$numPerPage;

		if($currentPage != 1) {
			$pages['currentPage'] = $currentPage*$numPerPage;
			$pages['numPerPage'] = $numPerPage;
		} else {
			$pages['currentPage'] = $currentPage;
			$pages['numPerPage'] = $numPerPage;
		}

		return $pages;
	}
}